package com.hspedu.newData.dmsxl.树.二叉树的遍历.递归;

import com.hspedu.newData.dmsxl.ds.TreeNode;
import com.hspedu.newData.dmsxl.贪心算法.easy.LC455分发饼干;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-13 
 * @Version: 1.0
 */

public class LC501二叉搜索树中的众数 {


    List<Integer> result = new ArrayList<>();
    int poseNum = 0;
    int count = 0;
    int maxCount = 0;
    public int[] findMode(TreeNode root) {
        if (root == null){
            int[] res = new int[0];
            return res;
        }

        int[] res = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            res[i] = result.get(i);
        }
        return res;
    }

    public void dfs(TreeNode root){
        if (root==null) return;
        dfs(root.left);
        update(root.val);
        dfs(root.right);
    }

    public void update(int val){
        if (val == poseNum){
            count++;
        }else {
            count = 1;
            poseNum = val;
        }
        if (count == maxCount){
            result.add(val);
        }
        if (count > maxCount){
            maxCount = count;
            result.clear();
            result.add(val);
        }


    }
}
